package com.zhouchb.test;

import javax.swing.text.html.HTMLWriter;
import java.util.Arrays;

/**
 * @Author Zhouchb
 * @Create 2021-11-2021/11/15 :15:51
 * @Description
 */
public class Merge {
    public static int[] tempArr;

    public static void main(String[] args) {
        int[] arr  = {9,9,1,3,8,7,5};
        tempArr = new int[arr.length];
        sort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(tempArr));
    }
    public static void sort(int[] arr,int l,int r){
        if (l>=r){
            return;
        }
        int mid = (l+r)/2;
        sort(arr,l,mid);
        sort(arr,mid+1,r);
        merge(arr,l,mid,r);
    }
    public static void merge(int[] arr,int l,int mid,int r){
        int i = l;
        int p1 = l;
        int p2 = mid+1;
        while (p1<=mid&&p2<=r){
            if (arr[p1]<arr[p2]){
                tempArr[i++] = arr[p1++];
            }else {
                tempArr[i++] = arr[p2++];
            }
        }
        while (p1<=mid){
            tempArr[i++] = arr[p1++];
        }
        while (p2<=r){
            tempArr[i++] = arr[p2++];
        }
        for (int j =l;j<=r;j++){
            arr[j] = tempArr[j];
        }
    }
}

